package com.yunxi.service.sap.dubbo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.baseDataToHub.pojo.sap.PartnerFundVO;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import com.yunxi.service.ReturnResultMsg;
import com.yunxi.ups.pojo.PartsAllotVO;
import com.yunxi.ups.pojo.PartsInventoryVO;
import com.yunxi.ups.pojo.PartsOrderVO;
import com.yunxi.ups.service.Ups2SapDubboService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.*;

/**
 * Created by Administrator on 2019/10/14.
 */
public class Ups2SapDubboServiceImpl implements Ups2SapDubboService {

    protected static final Logger LOGGER = LoggerFactory.getLogger(Ups2SapDubboServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Value("${esb.switch}")
    private String esbSwitch;
    @Value("${SAP.URL}")
    private String SAP_URL;
    @Value("${SAP.REPERTORY.URL}")
    private String SAP_REPERTORY_URL;

    @Override
    public ApiResult findPartneFundUPS2SAP(String var1) {
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(var1);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS获取SAP系统渠道资金");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.UPS_SAP_PARTNER_FUND);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok(ReturnResultMsg.findPartnerFundBySap());
        }
        Map dataMap=new HashMap();
        PartnerFundVO partnerFundVO=new PartnerFundVO();
        partnerFundVO.setKunnr(var1);
        List lists=new ArrayList();
        lists.add(partnerFundVO);
        dataMap.put("lists",lists);
        String partnerFund = JSONObject.toJSONString(dataMap);
        try {
            Map map = HttpUtil.sendPost(partnerFund, Constant.UPS_SAP_PARTNER_FUND, Constant.UPSPARTS, SAP_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(SAP_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag = String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")) {//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult = ResponseUtil.fail(400,"ESB错误!");
            } else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (String.valueOf(jsonResponse.get("code")).equals("E")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(400,String.valueOf(jsonResponse.get("desc")));
                }else {
                    apiResult = ResponseUtil.ok(String.valueOf(jsonResponse.get("return")));
                }
            }
        } catch (Exception e) {
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(), e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult = ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
        }
        return apiResult;
    }

    //配件库存
    @Override
    public ApiResult findPartsRepertoryUPS2SAP(String var1) {
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(var1);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS获取SAP系统配件库存");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.UPS_SAP_PARTS_REPERTORY);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok(ReturnResultMsg.findPartsRepertoryUPS2SAP());
        }
        Map dataMap=new HashMap();
        dataMap.put("matnr",var1);
        String data = JSONObject.toJSONString(dataMap);
        try {
            Map map = HttpUtil.sendPost(data, Constant.UPS_SAP_PARTS_REPERTORY, Constant.UPSPARTS, SAP_REPERTORY_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(SAP_REPERTORY_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag = String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")) {//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult = ResponseUtil.fail(400,"ESB错误!");
            } else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (String.valueOf(jsonResponse.get("code")).equals("E")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(400,String.valueOf(jsonResponse.get("desc")));
                }else {
                    apiResult = ResponseUtil.ok(String.valueOf(map.get("response")));
                }
            }
        } catch (Exception e) {
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(), e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult = ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
        }
        return apiResult;
    }

    //配件订单提交
    @Override
    public ApiResult sendPartsOrderUPS2SAP(PartsOrderVO var1) {
        String partsOrderData = JSONObject.toJSONString(var1);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(partsOrderData);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS提交配件订单提交给SAP");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.UPS_SAP_PARTS_ORDER);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok(ReturnResultMsg.sendPartsOrderUPS2SAP());
        }
        try {
            Map map = HttpUtil.sendPost(partsOrderData, Constant.UPS_SAP_PARTS_ORDER, Constant.UPSPARTS, SAP_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(SAP_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag = String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")) {//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult = ResponseUtil.fail(400,"ESB错误!");
            } else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (String.valueOf(jsonResponse.get("code")).equals("E")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(400,String.valueOf(jsonResponse.get("zmess")));
                }else {
                    apiResult = ResponseUtil.ok(String.valueOf(map.get("response")));
                }
            }
        } catch (Exception e) {
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(), e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult = ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
        }
        return apiResult;
    }

    @Override
    public ApiResult sendPartsAllotUPS2SAP(PartsAllotVO var1) {
        if(!StringUtils.isNotEmpty(var1.getRequisitionType())){
            var1.setRequisitionType("1");
        }
        String partsAllotData = JSONObject.toJSONString(var1);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(partsAllotData);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS提交配件调拨给SAP");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.UPS_SAP_PARTS_ALLOT);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok(ReturnResultMsg.sendPartsAllotUPS2SAP());
        }
        try {
            Map map = HttpUtil.sendPost(partsAllotData, Constant.UPS_SAP_PARTS_ALLOT, Constant.UPSPARTS, SAP_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(SAP_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag = String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")) {//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult = ResponseUtil.fail(400,"ESB错误!");
            } else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (String.valueOf(jsonResponse.get("code")).equals("E")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(400,String.valueOf(jsonResponse.get("zmess")));
                }else {
                    apiResult = ResponseUtil.ok(String.valueOf(map.get("response")));
                }
            }
        } catch (Exception e) {
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(), e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult = ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
        }
        return apiResult;
    }

    @Override
    public ApiResult sendPartsInventoryUPS2SAP(PartsInventoryVO var1) {
        String partsInventoryData = JSONObject.toJSONString(var1);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(partsInventoryData);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS提交配件入库给SAP");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.UPS_SAP_PARTS_INVENTORY);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok(ReturnResultMsg.sendPartsAllotUPS2SAP());
        }
        try {
            Map map = HttpUtil.sendPost(partsInventoryData, Constant.UPS_SAP_PARTS_INVENTORY, Constant.UPSPARTS, SAP_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(SAP_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag = String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")) {//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult = ResponseUtil.fail(400,"ESB错误!");
            } else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (String.valueOf(jsonResponse.get("code")).equals("E")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail(400,String.valueOf(jsonResponse.get("zmess")));
                }else {
                    apiResult = ResponseUtil.ok(String.valueOf(map.get("response")));
                }
            }
        } catch (Exception e) {
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(), e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult = ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
        }
        return apiResult;
    }
}
